# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.processChildDao import ProcessChildAPI

from . import app

CORS(app, supports_credentials=True)

'''项目订单子流程表 zzh_prochile API'''


# ----------     zzh_prochild表     YM      ------------
@app.route('/query-process-child/<int:index>', methods=['POST'])  # 查询zzh_prochild表。疑问：有没有特殊的查询规则需要更新
def queryProcessChild(index):
    queryResultOfDatabaseProcessChild, processChildListJson = ProcessChildAPI().queryProcessChild(index)  # 返回值的获取顺序要匹配

    if queryResultOfDatabaseProcessChild:
        return jsonify({"message": {"code": 1, "info": "已查询到订单子流程"}, "processChildListJson": processChildListJson,
                        "totalCount": queryResultOfDatabaseProcessChild})
    else:
        return jsonify({"message": {"code": 0, "info": "未查询到订单子流程"}})


@app.route('/add-process-child', methods=['POST'])  # prochild表添加记录
def addProcessChild():
    if not request.json:
        print request.json
        abort(400)
    addProcessChild = {
        'processChildName': request.json.get('processChildName', ""),  # processChildName  子流程的名称
        'processChildCode': request.json.get('processChildCode', ""),  # processChildCode 子流程步数
        'processChildStatus': request.json.get('processChildStatus', )  # processChildStatus  子流程状态
    }
    addProcessChildResult = ProcessChildAPI().addProcessChild(addProcessChild)
    if addProcessChildResult:
        return jsonify({"addProcessChildInfo": addProcessChild,
                        "massage": {"code": 1, "info": "addProcesssChild OK"}})  # 添加函数调用成功，返回添加的信息（不包括prochild_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addProcesssChild Failed"}})  # 添加函数调用失败


@app.route('/update-process-child/<int:processChildId>',
           methods=['POST'])  # 根据processChildId更新prochild表指定记录
def updateProcessChild(processChildId):
    if not request.json:
        print request.json
        abort(400)
    updateProcessChild = {
        'processChildName': request.json.get('processChildName', ""),  # processChildName  子流程的名称
        'processChildCode': request.json.get('processChildCode', ""),  # processChildCode 子流程步数
        'processChildStatus': request.json.get('processChildStatus', 0)  # processChildStatus  子流程状态
    }
    updateProcessChildResult = ProcessChildAPI().updateProcessChildByProcessId(updateProcessChild, processChildId)
    if updateProcessChildResult:
        return jsonify({"updateProcessChildInfo": updateProcessChild,
                        "massage": {"code": 1, "info": "updateProcesssChild OK"}})  # 添加函数调用成功，返回添加的信息（不包括prochild_id）
    else:
        return jsonify({"message": {"code": 0, "info": "updateProcesssChild Failed"}})  # 添加函数调用失败


@app.route('/delete-process-child/<int:processChildId>',
           methods=['DELETE'])  # 根据processChildId删除prochild表指定记录
def deleteProcessChild(processChildId):
    deleteProcessChildResult = ProcessChildAPI().deleteProcessChildById(processChildId)
    if deleteProcessChildResult:
        return jsonify({"message": {"code": 1, "info": "删除ProcesssChild记录操作成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除ProcesssChild记录操作失败！"}})
